package com.example.shortmessage.ui.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.View;

import androidx.annotation.NonNull;

import com.example.shortmessage.R;
import com.example.shortmessage.base.BaseActivity;
import com.example.shortmessage.base.BaseStyle;
import com.example.shortmessage.contract.LoginContract;
import com.example.shortmessage.data.constant.AccountConst;
import com.example.shortmessage.data.constant.FontsConst;
import com.example.shortmessage.data.constant.LoginConst;
import com.example.shortmessage.data.entity.Account;
import com.example.shortmessage.databinding.ActivityLoginBinding;
import com.example.shortmessage.interfaces.DialogOnClickInterface;
import com.example.shortmessage.permission.PermissionHelper;
import com.example.shortmessage.permission.PermissionInterface;
import com.example.shortmessage.presenter.LoginPresenter;
import com.example.shortmessage.tips.dialog.NormalDialog;
import com.example.shortmessage.tips.dialog.WaitingDialog;

/**
 * @author 27638
 */
public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginContract.View<LoginContract.Presenter>, LoginContract.Presenter>
        implements LoginContract.View<LoginContract.Presenter>, View.OnClickListener, PermissionInterface, DialogOnClickInterface {

    private static final int REQUEST_CODE = 201;
    private PermissionHelper mPermissionHelper;
    private final String[] permission = new String[]{
            Manifest.permission.READ_SMS,
            Manifest.permission.SEND_SMS,
            Manifest.permission.RECEIVE_SMS,
            Manifest.permission.READ_PHONE_STATE
    };

    public void LoginActivityInstance(Context context){
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected LoginContract.Presenter initPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    protected ActivityLoginBinding getLayoutView() {
        return ActivityLoginBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        mBinding.etUsername.setText(mPresenter.getUsername());
        mBinding.etPassword.setText(mPresenter.getPassword());
        mBinding.title.tvTitle.setText(LoginConst.TITLE);
        mPermissionHelper = new PermissionHelper(this, this);
        //发起权限申请
        mPermissionHelper.requestPermissions(permission);
        mBinding.tvRePassword.setOnClickListener(this);
        mBinding.tvRegister.setOnClickListener(this);
        mBinding.btnLogin.setOnClickListener(this);
    }

    @Override
    protected void setStyle() {
        mBinding.title.tvTitle.setVisibility(View.VISIBLE);
        //下划线
        mBinding.tvRePassword.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
        mBinding.tvRegister.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
        setStyleSize();
    }

    private void setStyleSize() {
        BaseStyle baseStyle = new BaseStyle();
        baseStyle.set(this, mBinding.tvRePassword, FontsConst.TV_MIN_CONTENT);
        baseStyle.set(this, mBinding.tvRegister, FontsConst.TV_MIN_CONTENT);
        baseStyle.set(this, mBinding.title.tvTitle, FontsConst.TV_TITLE);
        baseStyle.set(this, mBinding.tvTitle, FontsConst.TV_TITLE);
        baseStyle.set(this, mBinding.tvSecondaryTitle, FontsConst.TV_CONTENT);
        baseStyle.set(this, mBinding.etUsername, FontsConst.TV_CONTENT);
        baseStyle.set(this, mBinding.etPassword, FontsConst.TV_CONTENT);
    }
    /************************获取权限***********************************/
    @Override
    public int getPermissionsRequestCode() {
        return REQUEST_CODE;
    }

    @Override
    public void requestPermissionsSuccess() {

    }

    @Override
    public void requestPermissionsFail() {
        NormalDialog.getInstance(this,this)
                .createDialog("权限警告","缺少权限可能会导致应用运行不正常","继续授权");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        mPermissionHelper.requestPermissionsResult(requestCode,permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    /***************************对话框回调*************************************/
    @Override
    public void interfacePositive() {
        //发起权限申请
        mPermissionHelper.requestPermissions(permission);
    }

    @Override
    public void interfaceNegative() {

    }
    /***************************************************************/
    @Override
    public void setPresenter(@NonNull LoginContract.Presenter presenter) { 
        mPresenter = presenter;
    }

    @Override
    public void showLoading() {
        WaitingDialog.getInstance().show(this);
    }

    @Override
    public void hideLoading() {
        WaitingDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(@NonNull View v) {
        Intent intent = new Intent(this,AccountActivity.class);
        Account account = new Account();
        switch (v.getId()){
            case R.id.tv_re_password :
                account.setRegister(false);
                intent.putExtra(AccountConst.SIGN,account);
                startActivity(intent);
                break;
            case R.id.tv_register :
                account.setRegister(true);
                intent.putExtra(AccountConst.SIGN,account);
                startActivity(intent);
                break;
            case R.id.btn_login:
                String username = mBinding.etUsername.getText().toString().trim();
                String password = mBinding.etPassword.getText().toString().trim();
                if(username.isEmpty()){
                    mBinding.etUsername.setError(LoginConst.NULL);
                    return;
                }else if(password.isEmpty()){
                    mBinding.etPassword.setError(LoginConst.NULL);
                    return;
                }
                mPresenter.login(username, password);
                break;
            default:
        }
    }
}
